package com.glowdayz.glowmee.bc;

import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.DateTime;
import com.glowdayz.common.MultipartBox;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.MultipartBox.FileInfo;
import com.glowdayz.glowmee.cmd.PouchCMD;
import com.glowdayz.glowmee.dao.BrandDAO;
import com.glowdayz.glowmee.dao.PouchCategoryDAO;
import com.glowdayz.glowmee.dao.PouchDAO;

public class UpdateEditerPouchBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public UpdateEditerPouchBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		int idPouch = Integer.parseInt(request.getParameter("id"));
		
		try {
			request.setAttribute("cmd", PouchDAO.getInstance().getEditerPouch(idPouch));
			request.setAttribute("pouchCategory", PouchCategoryDAO.getInstance().getPouchCategoryList(1, -1, "pouchCategoryText"));
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, -1, "brandTitle", null));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/updateEditerPouch.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		MultipartBox mbox = MultipartBox.getInstance();
		FileInfo finfo = mbox.upload2(request);
		
		int idPouch = mbox.getInt("id");
		int isDisplay = mbox.getInt("isDisplay");
		int isEditerPick = mbox.getInt("isEditerPick");
		String pouchTitle = mbox.getValue("pouchTitle");
		int pouchCategory = mbox.getInt("pouchCategory");
		int pouchBrand = mbox.getInt("pouchBrand");
		String pouchText = mbox.getValue("pouchText");
		int[] product = new int[10];
		for (int i=0; i<10; i++) product[i] = mbox.getInt("product" + i);
		
		try {
			PouchCMD cmd = PouchDAO.getInstance().getEditerPouch(idPouch);
			FileInfo image = cmd.getImage();
			if (image != null && finfo != null) {
				DBHandler.getInstance().beginTrans();
				if (finfo.getFieldName().equals("editImg")) {
					String edit = "UPDATE attachefile SET fileOrgName=?, fileSaveName=?, fileDir=?, fileSize=?, fileType=?, modified_date=? "
							+ "WHERE idAttacheFile=?";
					PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(edit);
					pstmt.setString(1, finfo.getFileOrgName());
					pstmt.setString(2, finfo.getFileSaveName());
					pstmt.setString(3, finfo.getFileDir());
					pstmt.setLong(4, finfo.getFileSize());
					pstmt.setString(5, finfo.getFileType());
					pstmt.setString(6, DateTime.getInstance().getNowDateTimeString());
					pstmt.setInt(7, image.getIdAttacheFile());
					pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
					finfo = null;
					File f = new File(image.getFileDir() + "/" + image.getFileSaveName());
					if (f.exists()) {
						f.delete();
					}
					finfo = null;
				}
				DBHandler.getInstance().commit();
			}
		} catch (Exception e) {
			DBHandler.getInstance().rollback();
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		try {
			PouchDAO.getInstance().updateEditerPouch(new PouchCMD(
					idPouch, pouchCategory, pouchTitle, pouchText, isDisplay, isEditerPick, finfo, pouchBrand), product);
		} catch (Exception e) {
			DBHandler.getInstance().rollback();
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("editerPouch", PouchDAO.getInstance().getEditerPouchList(page, 10, null, null));
			request.setAttribute("display", DBHandler.getInstance().getCount(
					"pouch p, register r", "p.idPouch", "p.idRegister=r.idRegister AND r.idRegister=1 AND p.isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
					"pouch p, register r", "p.idPouch", "p.idRegister=r.idRegister AND r.idRegister=1 AND p.isDisplay='0'"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/listEditerPouch.jsp");
		dpr.forward(request, response);
	}

}